package com.dhl.fin.api.controller;

import com.dhl.fin.api.common.controller.CommonController;
import com.dhl.fin.api.common.dto.ApiResponse;
import com.dhl.fin.api.common.dto.QueryDto;
import com.dhl.fin.api.common.util.*;
import com.dhl.fin.api.domain.RemindTask;
import com.dhl.fin.api.domain.RemindType;
import com.dhl.fin.api.dto.RemindTypeReqDTO;
import com.dhl.fin.api.enums.RemindTypeEnum;
import com.dhl.fin.api.service.RemindTaskService;
import com.dhl.fin.api.service.RemindTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author CuiJianbo
 * @since 2025/5/7
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@Slf4j
@RequestMapping("remindtype")
public class RemindTypeController extends CommonController<RemindType> {

    @Autowired
    private RemindTaskService remindTaskService;

    @Autowired
    private RemindTypeService remindTypeService;


    @ResponseBody
    @PostMapping("updateTaskType")
    public ApiResponse updateTaskType(@RequestBody RemindTypeReqDTO remindTypeReqDTO) throws Exception {

        remindTaskService.updateTaskType(remindTypeReqDTO.getTaskTypes());

        return ApiResponse.success();
    }

    @ResponseBody
    @GetMapping("change/sort")
    public ApiResponse changePositionSort(Long id, Integer sort, String position) throws Exception {
        String unicode = WebUtil.getLoginUser().getUuid();
        remindTypeService.changePosition(id, sort, position);
        List<RemindType> remindTypes = remindTypeService.getAllRemindTypes(unicode);
        return ApiResponse.success(remindTypes);
    }


    @ResponseBody
    @GetMapping("addTaskType")
    public ApiResponse addTaskType(String name, String icon) throws Exception {
        String unicode = WebUtil.getLoginUser().getUuid();

        RemindType dbData = commonService.get(QueryDto.builder().available()
                .addWhere("unicode='" + unicode + "'")
                .addWhere("remind_type_name='" + name + "'")
                .build());

        if (ObjectUtil.notNull(dbData)) {
            return ApiResponse.error("名字不能重复");
        }

        dbData = commonService.get(QueryDto.builder().available()
                .addWhere("unicode='" + unicode + "'")
                .addOrder("sort desc")
                .build());
        int sortNum = ObjectUtil.notNull(dbData) ? dbData.getSort() + 1 : 1;
        RemindType remindType = new RemindType();
        remindType.setUnicode(unicode);
        remindType.setIcon(icon);
        remindType.setRemindTypeName(name);
        remindType.setSort(sortNum);
        remindType.setIsActive(true);
        remindType.setIsFix(false);

        commonService.saveOrUpdate(remindType);
        return ApiResponse.success();
    }

    @ResponseBody
    @GetMapping("disable/{id}")
    public ApiResponse disableRemindType(@PathVariable Long id, Boolean active) throws Exception {
        remindTypeService.updateFieldById(id, "isActive", active);
        return ApiResponse.success();
    }

    @ResponseBody
    @GetMapping("types")
    public ApiResponse getTypes() throws Exception {
        String unicode = WebUtil.getLoginUser().getUuid();
        List<RemindType> remindTypes = remindTypeService.getAllRemindTypes(unicode);
        if (CollectorUtil.isEmpty(remindTypes)) {
            remindTypes = Arrays.stream(RemindTypeEnum.values())
                    .map(p -> {
                        RemindType remindType = new RemindType();
                        remindType.setRemindTypeName(p.getName());
                        remindType.setIcon(p.getCode());
                        remindType.setSort(p.getSort());
                        remindType.setIsFix(true);
                        remindType.setIsActive(true);
                        return remindType;
                    }).collect(Collectors.toList());
        }
        return ApiResponse.success(remindTypes);
    }


}
